home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 7 / Apprentice-Release7.iso / Source Code / C / Applications / Moscow ML 1.42 / src / mosmllib / Int.sml < prev    next >
Encoding:
Text File  |  1997-08-18  |  4.3 KB  |  118 lines  |  [TEXT/Moml]

  1. (* Int -- new basis 1995-03-19, 1996-04-01 *)
  2.  
  3. type int = int
  4.  
  5. (* 32-bit architecture: *)
  6. val precision = SOME 31;        
  7. val minInt    = SOME ~1073741824;
  8. val maxInt    = SOME  1073741823;
  9.  
  10. local 
  11.     open StringCvt
  12.     (* Below, 48 = Char.ord #"0" and 55 = Char.ord #"A" - 10. *)
  13.     fun decval c = Char.ord c - 48;
  14.     fun hexval c = 
  15.         if #"0" <= c andalso c <= #"9" then Char.ord c - 48
  16.         else (Char.ord c - 55) mod 32;
  17.     fun prhex i = if i < 10 then Char.chr(i + 48) else Char.chr(i + 55)
  18.     fun skipWSget getc source = getc (dropl Char.isSpace getc source)
  19.  
  20.     fun conv radix i = 
  21.         let fun h 0 res = res
  22.               | h n res = h (n div radix) (prhex (n mod radix) :: res)
  23.             fun tostr n = h (n div radix) [prhex (n mod radix)]
  24.         in String.implode (if i < 0 then #"~" :: tostr (~i) else tostr i) end
  25. in
  26.     fun scan radix getc source =
  27.         let open StringCvt
  28.             val (isDigit, factor) = 
  29.                 case radix of
  30.                     BIN => (fn c => (#"0" <= c andalso c <= #"1"),  2)
  31.                   | OCT => (fn c => (#"0" <= c andalso c <= #"7"),  8)
  32.                   | DEC => (Char.isDigit,                          10)
  33.                   | HEX => (Char.isHexDigit,                       16)
  34.             fun dig1 sgn NONE             = NONE
  35.               | dig1 sgn (SOME (c, rest)) = 
  36.                 let fun digr res src = 
  37.                     case getc src of
  38.                         NONE           => SOME (sgn * res, src)
  39.                       | SOME (c, rest) => 
  40.                             if isDigit c then 
  41.                                 digr (factor * res + hexval c) rest
  42.                             else 
  43.                                 SOME (sgn * res, src)
  44.                 in if isDigit c then digr (hexval c) rest else NONE end     
  45.             fun getdigs sgn after0 inp = 
  46.                 case dig1 sgn inp of
  47.                     NONE => SOME(0, after0)
  48.                   | res  => res
  49.             fun hexopt sgn NONE                 = NONE
  50.               | hexopt sgn (SOME(#"0", after0)) =
  51.                 if radix <> HEX then getdigs sgn after0 (getc after0)
  52.                 else
  53.                     (case getc after0 of
  54.                          NONE             => SOME(0, after0)
  55.                        | SOME(#"x", rest) => getdigs sgn after0 (getc rest)
  56.                        | SOME(#"X", rest) => getdigs sgn after0 (getc rest)
  57.                        | inp              => getdigs sgn after0 inp)
  58.               | hexopt sgn inp = dig1 sgn inp
  59.             fun sign NONE                = NONE
  60.               | sign (SOME (#"~", rest)) = hexopt ~1 (getc rest)
  61.               | sign (SOME (#"-", rest)) = hexopt ~1 (getc rest)
  62.               | sign (SOME (#"+", rest)) = hexopt  1 (getc rest)
  63.               | sign inp                 = hexopt  1 inp
  64.         in sign (skipWSget getc source) end;
  65.             
  66.     fun fmt BIN = conv 2
  67.       | fmt OCT = conv 8
  68.       | fmt DEC = conv 10
  69.       | fmt HEX = conv 16
  70.  
  71.     (* It should hold that: toString = fmt DEC = conv 10 *)     
  72.     prim_val toString : int -> string = 1 "sml_string_of_int";
  73.  
  74.     val fromString = scanString (scan DEC)
  75. end
  76.  
  77. fun pow (x, n) = 
  78.     let fun h 0 res = res
  79.           | h i res = if i mod 2 = 0 then h (i div 2) (res * res)
  80.                       else h (i-1) (x * res)
  81.     in 
  82.         if n < 0 then 
  83.             if x = 0 then raise Domain else 1 div (h (~n) 1)
  84.         else h n 1
  85.     end
  86.  
  87. val ~       : int -> int        = ~;
  88. val op *    : int * int -> int  = op *;
  89. val op div  : int * int -> int  = op div;
  90. val op mod  : int * int -> int  = op mod;
  91.  
  92. local 
  93.     prim_val quot_ : int -> int -> int = 2 "quot";
  94.     prim_val rem_  : int -> int -> int = 2 "rem"
  95. in
  96.     fun quot(x, y) = quot_ x y
  97.     fun rem(x, y)  = rem_ x y
  98. end
  99.  
  100. val op +    : int * int -> int  = op +;
  101. val op -    : int * int -> int  = op -;
  102. val op >    : int * int -> bool = op >;
  103. val op >=   : int * int -> bool = op >=;
  104. val op <    : int * int -> bool = op <;
  105. val op <=   : int * int -> bool = op <=;
  106. val abs     : int -> int = abs;
  107. fun min (x, y) = if x < y then x else y : int;
  108. fun max (x, y) = if x < y then y else x : int;
  109. fun sign i = if i > 0 then 1 else if i < 0 then ~1 else 0;
  110. fun compare (x, y: int) = if x<y then LESS else if x>y then GREATER else EQUAL;
  111.  
  112. fun sameSign (i, j) = sign i = sign j;
  113.  
  114. fun toInt   i   = i;
  115. fun fromInt i   = i;
  116. fun toLarge   i = i;
  117. fun fromLarge i = i;
  118.